﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GUESTSMANAGEMENT.Controller
{
    public class GuestsList
    {
        public static gmlistguests getByListsGuests(int id)
        {
            //seta a usuário
            gmlistguests objListsGuests = null;

            using (guestsmanagementEntities context = new guestsmanagementEntities())
            {
                objListsGuests =
                (from listsguests in context.gmlistguests
                 where listsguests.CDLISTGUESTS == id
                 select listsguests).FirstOrDefault();
            }
            return objListsGuests;
        }

        public static List<gmlistguests> getList()
        {
            //seta a variável lista            
            List<gmlistguests> lista = null;

            using (guestsmanagementEntities context = new guestsmanagementEntities())
            {
                IQueryable<gmlistguests> query =
                from listguests in context.gmlistguests
                select listguests;
                lista = query.ToList();
            }
            return lista;
        }

        public static List<object[]> getGridList()
        {
            List<gmlistguests> ListGuests = getList();
            List<gmguests> Guests = Controller.Guests.getGridList();
            List<gmevent> Event = Controller.Event.getGridList();
            List<gmuser> User = Controller.User.getGridList();
            List<object[]> list = new List<object[]>();

            int ListGuestId;
            string userName = "", guestsName = "", guestsPhone = "", eventName = "";

            for (int l = 0; l < ListGuests.Count; l++)
            {
                ListGuestId = -1;
                userName = "";
                guestsName = "";
                guestsPhone = "";
                eventName = "";

                gmlistguests guestlist = Controller.GuestsList.getByListsGuests(Convert.ToInt32(ListGuests[l].CDLISTGUESTS));

                ListGuestId = Convert.ToInt32(guestlist.CDLISTGUESTS);

                for (int u = 0; u < User.Count; u++)
                {
                    gmuser user = Controller.User.getByUser(Convert.ToInt32(User[u].CDUSER));

                    if (user.CDUSER == guestlist.GMUSER_CDUSER)
                    {
                        userName = user.NMUSER;

                        break;
                    }
                }

                for (int g = 0; g < Guests.Count; g++)
                {

                    gmguests guests = Controller.Guests.getByGuests(Convert.ToInt32(Guests[g].CDGUESTS));

                    if (guests.CDGUESTS == guestlist.GMGUESTS_CDGUESTS)
                    {
                        guestsName = guests.NMGUESTS;
                        guestsPhone = guests.NMPHONE;

                        break;
                    }
                }

                for (int e = 0; e < Event.Count; e++)
                {
                    gmevent gevent = Controller.Event.getByEvent(Convert.ToInt32(Event[e].CDEVENT));

                    if (gevent.CDEVENT == guestlist.GMEVENT_CDEVENT)
                    {
                        eventName = gevent.NMEVENT;

                        break;
                    }
                }
                if (ListGuestId != -1)
                {
                    list.Add(new object[] { ListGuestId, guestsName, guestsPhone, userName, eventName });
                }
            }
            return list;
        }
    }
}
